/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Whitemice.ZOGI.GtkForms;
using Gtk;

namespace Whitemice.ZOGI.GtkFolios
{
  public partial class ContactSearchDialog  : Gtk.Dialog, IZOGIWidget
  {
		protected int									contactId;
		public delegate void 				  SetContactIdHandler(int contactId);
		public SetContactIdHandler    SetContactId;
		protected MultiResultCallback callback;
    protected ContactQBEDialog                    qbeDialog;
		
    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="parent">
    /// A <see cref="Window"/>
    /// </param>
    /// <param name="contactId">
    /// A <see cref="System.Int32"/>
    /// </param>
    /// <param name="title">
    /// A <see cref="System.String"/>
    /// </param>
		public ContactSearchDialog(Window parent, int contactId, string title)
			: base(title, parent, Gtk.DialogFlags.DestroyWithParent)
		{			
			this.contactId = contactId;
			this.Build();
			Title = title;
			contactList.NodeSelection.Changed += new System.EventHandler(OnSelectionChanged);
			callback = new MultiResultCallback(this.ReceiveDataCallback);
		}    

    /// <value>
    /// Controls availability of pop-up menu via right-click
    /// </value>
		public bool PopupEnabled
		{
			get { return contactList.PopupEnabled; }
			set { contactList.PopupEnabled = value; }
		} /* End PopupEnabled */
		
    /// <summary>
    /// The entity selected in the contact list has changed
    /// </summary>
    /// <param name="sender">
    /// A <see cref="System.Object"/>
    /// </param>
    /// <param name="args">
    /// A <see cref="System.EventArgs"/>
    /// </param>
		protected void OnSelectionChanged (object sender, System.EventArgs args)
		{
			Gtk.NodeSelection	selection;
			ContactListNode		node;
			
			selection = (Gtk.NodeSelection)sender;
			if (selection.SelectedNode == null)
			{
				contactId = 0;
			} else
				{
      		node = (ContactListNode)selection.SelectedNode;
      		contactId = node.Contact.ObjectId;
      	}
      this.ZLOG("Selected ContactId#{0}", contactId);
		}	/* End OnSelectionChanged */
		
		public void ReceiveRPCCall(RPCEventArgs args)
		{
      if (args.HasResults)
        contactList.Fill(args.Payload, null);
		  this.Modal = false;
		} /* End SearchResultCallback */

    /// <summary>
    /// Find clicked, prepare and issue search
    /// </summary>
    /// <param name="sender">
    /// A <see cref="System.Object"/>
    /// </param>
    /// <param name="e">
    /// A <see cref="System.EventArgs"/>
    /// </param>
		protected void OnFindClicked(object sender, System.EventArgs e)
		{
      ContactSearchCommand  command;

      command = new ContactSearchCommand(0, "ContactSearch");
      command.Callback = callback;
      command.AddORCriteria("firstName",
                            string.Format("*{0}*", searchCriteria.Text),
                            true,
                            false);
      command.AddORCriteria("lastName",
                            string.Format("*{0}*", searchCriteria.Text),
                            true,
                            false);
      Server.Connection.RunCommand(command);
			this.Modal = true;
		} /* End OnFindClicked */
  
    /// <summary>
    /// Empty list
    /// </summary>
    /// <param name="sender">
    /// A <see cref="System.Object"/>
    /// </param>
    /// <param name="e">
    /// A <see cref="System.EventArgs"/>
    /// </param>
		protected void OnClearClicked(object sender, System.EventArgs e)
		{
			contactId = 0;
			contactList.NodeStore.Clear();
		} /* End OnClearClicked */

    /// <summary>
    /// Ok clicked, return selected list entry to the owner and disappear
    /// </summary>
    /// <param name="sender">
    /// A <see cref="System.Object"/>
    /// </param>
    /// <param name="e">
    /// A <see cref="System.EventArgs"/>
    /// </param>
		protected void OnOkClicked(object sender, System.EventArgs e)
		{
      this.ZLOG("OK: ContactId#{0}", contactId);
			this.SetContactId(contactId);
			this.Hide();
		} /* End OnOkClicked */
		
    /// <summary>
    /// Cancel clicked, disappear without pushing selected item to owner
    /// </summary>
    /// <param name="sender">
    /// A <see cref="System.Object"/>
    /// </param>
    /// <param name="e">
    /// A <see cref="System.EventArgs"/>
    /// </param>
		protected void OnCancelClicked(object sender, System.EventArgs e)
		{
			this.Hide();
		} /* End OnCancelClicked */

		protected virtual void OnQBEClicked (object sender, System.EventArgs e)
		{
      if (qbeDialog == null)
      {
        qbeDialog = new ContactQBEDialog(
          null, 
          Gtk.DialogFlags.DestroyWithParent);
        qbeDialog.OnQBEDialogOk += new System.EventHandler(OnQBEOk);
        qbeDialog.OnQBEDialogCancel += new System.EventHandler(OnQBECancel);
        qbeDialog.Destroyed += new System.EventHandler(OnQBEDestroyed);
      } else
        {
          qbeDialog.Show();
          qbeDialog.Present();
        }
		}

    protected virtual void OnQBEOk(object sender, EventArgs args)
    {
      qbeDialog.Hide();
      var command = qbeDialog.GetQuery(65535, "ContactSearch");
      command.Callback = callback;
      Server.Connection.RunCommand(command);
      qbeDialog.Hide();
    } // end OnQBEOk
    
    protected virtual void OnQBECancel(object sender, EventArgs args)
    {
      qbeDialog.Hide();
    } // end OnQBECancel
    
    protected virtual void OnQBEDestroyed(object sender, EventArgs args)
    {
      qbeDialog = null;
    } // end OnQBEDestroyed
  }
}
